﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using C1.Web.Wijmo.Controls.C1Pager;
using Microsoft.Practices.ServiceLocation;
using SolrNet;
using SolrNet.Commands.Parameters;
using SolrNet.Mapping;
using System.Text.RegularExpressions;

namespace mysch
{
    public partial class gosearch : System.Web.UI.Page
    {
        public string pidx = "0";
        public string q = "tgbus";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["q"] != null)
                {
                    q = Request.QueryString["q"].ToString();
                    C1InputText1.Text = q;
                    if (Request.QueryString["pidx"] != null)
                    {
                        pidx = Request.QueryString["pidx"].ToString();
                        Label1.Text = "当前为" + (int.Parse(pidx) + 1).ToString() + "页";
                        bind();
                    }
                }
                else
                {
                    pidx = "0";
                    bind();
                }
            }
           
           
           /* C1Pager1.PageCount = 100;
            Label1.Text = pidx;*/
        }

        private void bind()
        {
            //Startup.Init<prt>("http://192.168.1.157:8983/solr");
         
            /*  var mapper = new AllPropertiesMappingManager();
              mapper.SetUniqueKey(typeof(prt).GetProperty("id"));*/
            var solr = ServiceLocator.Current.GetInstance<ISolrOperations<prt>>();
            QueryOptions qo = new QueryOptions();
            qo.Rows = 20;
            /*List<SolrQuery> sqs = new List<SolrQuery>();
            sqs.Add(new SolrQuery(url:\"^http://www.letv.com/ptv/vplay\""));*/
            qo.FilterQueries.Add(new SolrQuery("url:\"^http://www.letv.com/ptv/vplay\""));
            qo.Start = int.Parse(pidx);
            //   var results = solr.Query(new SolrQueryByField("url", "tgbus"));
            ISolrQueryResults<prt> results = solr.Query(q, qo);
          //  dataGridView1.AutoGenerateColumns = true;
           // MessageBox.Show(results.NumFound.ToString());
            //   dataGridView1.Rows.Clear();
            C1GridView1.DataSource = results.ToList();
            C1GridView1.DataBind();
            if (results.NumFound>20)
            {
                 C1Pager1.PageCount = results.NumFound/20;
            }
            else
            {
                C1Pager1.PageCount = 1;
            }
            C1Pager1.PageIndex = int.Parse(pidx);
            Label2.Text ="共找到"+ results.NumFound.ToString()+"项";
        }

        protected void C1Pager1_PageIndexChanged(object sender, EventArgs e)
        {
            C1Pager cp = sender as C1Pager;
            Label1.Text ="当前为"+( cp.PageIndex+1).ToString()+"页";
           // Response.Redirect("gosearch.aspx?pidx="+cp.PageIndex.ToString());
            pidx = cp.PageIndex.ToString();
            q = C1InputText1.Text;
            bind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.C1InputText1.Text != "")
            {
                Response.Redirect("gosearch.aspx?q=" + this.C1InputText1.Text + "&pidx=0");
            }
        }
        public static bool IsChinese(string CString)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(CString, @"[\u4e00-\u9fa5]");
           // return Regex.IsMatch(CString, @"^[\u4e00-\u9fa5]+$");
        }
    }
}